package com.example.howard.xiao7music.ui.Activity.RemoteActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.howard.xiao7music.MyApplication;
import com.example.howard.xiao7music.R;
import com.example.howard.xiao7music.bean.SongRankingBean;
import com.example.howard.xiao7music.model.getbasedata.Injection;
import com.example.howard.xiao7music.presenter.RemoteSongPresenter;
import com.example.howard.xiao7music.service.DownloadService;
import com.example.howard.xiao7music.service.MusicPlayerService;
import com.example.howard.xiao7music.ui.BottomPlayer;
import com.example.howard.xiao7music.ui.adapter.LocalAdapter.FolderSongAdapter;
import com.example.howard.xiao7music.ui.adapter.RemoteAdapter.RankingListAdapter;
import com.example.howard.xiao7music.ui.adapter.RemoteAdapter.RankingSongAdapter;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RankingSongActivity extends Activity {

    private AdapterView.OnItemClickListener listener;
    private RemoteSongPresenter remoteSongPresenter;
    private MyApplication myApplication;
    private List<Map<String, Object>> songlist = new ArrayList<>();
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private RankingSongAdapter myAdapter;
    private TextView title;
    private ListView listView;
    private BottomPlayer bottomPlayer;
    private int num;
    private SongRankingBean songRankingBean;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ranking_song);
        myApplication = (MyApplication) getApplication();
        remoteSongPresenter = new RemoteSongPresenter(new RemoteSongPresenter.callback() {
            @Override
            public void getRankingSongList(List<Map<String, Object>> list) {
                songlist=list;
                myApplication.setSongdatalist(songlist);
                Log.e("activity",songlist.get(1).toString());
                refreshListView();
            }

            @Override
            public void getSearchBySinger(List<Map<String, Object>> songlist) {

            }

            @Override
            public void getSearchBySong(List<Map<String, Object>> songlist) {

            }
        });
        Intent intent = this.getIntent();

        num = intent.getIntExtra("num", -1);
        init(num);
        net();
        remoteSongPresenter.getRankingSong(num);

//        remoteSongPresenter.test();

//        refreshListView();
        bottomPlayer = new BottomPlayer();
        fragmentManager = getFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.ranking_song_bottomplaybar, bottomPlayer);
        transaction.commit();

    }



    private void init(int num) {
        title = (TextView) findViewById(R.id.ranking_song_titel);
        listView = (ListView) findViewById(R.id.ranking_song_list);
        title.setText(titleName(num)+" TOP50");
    }

    private String titleName(int num) {
        String rankingName;
        switch (num) {
            case 0:
                rankingName = "新歌榜";
                break;
            case 1:
                rankingName = "热歌榜";
                break;
            case 2:
                rankingName = "欧美金曲榜";
                break;
            case 3:
                rankingName = "经典老歌榜";
                break;
            case 4:
                rankingName = "情歌对唱榜";
                break;
            case 5:
                rankingName = "网络歌曲榜";
                break;
            default:
                rankingName = "不存在该排行榜";
                break;
        }
        return rankingName;
    }

    private void refreshListView() {
        myAdapter = new RankingSongAdapter(this, songlist, new RankingSongAdapter.RankingSongCallback() {
            @Override
            public void download(int position) {
                dialog(position);
            }

        });
        listView.setAdapter(myAdapter);
        listener = resetClickListener();
        listView.setOnItemClickListener(listener);
//        myAdapter.notifyDataSetChanged();
    }


    private void net(){
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()
                .penaltyLog()
                .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects()
                .detectLeakedClosableObjects()
                .penaltyLog()
                .penaltyDeath()
                .build());
    }


    private AdapterView.OnItemClickListener resetClickListener() {
        AdapterView.OnItemClickListener songListclickListener = new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                //异步延迟，在回调中set
//                myApplication.setSongdatalist(songlist);
                bottomPlayer.callByList(arg2);
            }
        };
        return songListclickListener;
    }

    @Override
    protected void onResume() {
        super.onResume();
        getBottomPlayerState(bottomPlayer);
    }

    @Override
    protected void onPause() {
        super.onPause();
        setBottomPlayerState(bottomPlayer.getMusicPlayerService(), bottomPlayer.getSongList(),
                bottomPlayer.getMap(), bottomPlayer.getPosition());
    }

    //获取bottomplayer的状态
    private void getBottomPlayerState(BottomPlayer player) {
        if (myApplication.musicPlayerService != null) {
            player.setMusicPlayerService(myApplication.musicPlayerService);
        }
        if (myApplication.songdatalist.size() > 0) {
            player.setSongList(myApplication.songdatalist);
            player.setMap(myApplication.map);
            player.setPosition(myApplication.position);
        }

    }

    //上传bottomplayer的状态
    private void setBottomPlayerState(MusicPlayerService service, List<Map<String, Object>> songlist,
                                      Map<String, Object> map, int pos) {
        myApplication.setMusicPlayerService(service);
        myApplication.setSongdatalist(songlist);
        myApplication.setMap(map);
        myApplication.setPosition(pos);
    }

    protected void dialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确认要下载歌曲到本地吗？");

        builder.setTitle("请求下载歌曲");

        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent=new Intent(RankingSongActivity.this, DownloadService.class);
//                Log.e("Activity",rankingsongbeanlist.get(position).getSong_id());
                intent.putExtra("songID",songlist.get(position).get("ID").toString());
                startService(intent);
                dialog.dismiss();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        builder.create().show();
    }



    private static final int SHOW_RANKINGLIST = 123;
    public class MyHandler extends Handler {

        private Context context;

        public MyHandler(Context context) {
            this.context = context;
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case SHOW_RANKINGLIST:

                    break;
            }

        }
    }

}
